﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

public partial class NhaThau : System.Web.UI.Page
{
    SqlParameter[] p;
    clsXuLy ex = new clsXuLy();
    string frmName = "frmNhaThau";
    public string Header = System.Configuration.ConfigurationSettings.AppSettings["Header"].ToString();
    static DataTable dt;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Title = "Quản lí nhà thầu | " + Header;
            btnCancel_Click(sender, e);
        }

    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        try
        {
            Guid ID = Guid.NewGuid();
            p = new SqlParameter[3];
            p[0] = new SqlParameter("@IDs", ID);
            p[0].SqlDbType = SqlDbType.UniqueIdentifier;
            p[1] = new SqlParameter("@TenNhaThau", txtTenNhaThau.Text.Trim());
            p[1].SqlDbType = SqlDbType.NVarChar;
            p[2] = new SqlParameter("@MaNhaThau", txtMaNhaThau.Text.Trim());
            p[2].SqlDbType = SqlDbType.NVarChar;
            ex.ExecuteNonQuerry("sp_vnttblnhathau_Add", p);

            for (int j = 0; j < gvGoiThau.Rows.Count; j++)
            {
                CheckBox ck = (CheckBox)gvGoiThau.Rows[j].Cells[1].FindControl("ckGoiThau");
                if (ck.Checked)
                {
                    Label lbID = (Label)gvGoiThau.Rows[j].Cells[0].FindControl("lbID");

                    Guid IDGoiThau = new Guid(lbID.Text.Trim());
                    p = new SqlParameter[3];
                    p[0] = new SqlParameter("@IDs", Guid.NewGuid());
                    p[0].SqlDbType = SqlDbType.UniqueIdentifier;
                    p[1] = new SqlParameter("@IDNhaThau", ID);
                    p[1].SqlDbType = SqlDbType.UniqueIdentifier;
                    p[2] = new SqlParameter("@IDGoiThau", IDGoiThau);
                    p[2].SqlDbType = SqlDbType.UniqueIdentifier;
                    ex.ExecuteNonQuerry("sp_vnttblnhathaugoithau_Add", p);
                }
            }
            btnCancel_Click(sender, e);
            gstGetMess("Lưu thành công", "");
        }
        catch (Exception ax)
        {
            gstGetMess("Có lỗi trong quá trình lưu, xem trong file log", "");
            ex.WriteLog(Server.MapPath("Log.txt"), DateTime.Now.ToString() + "\tNhà thầu\t" + ax.Message);
        }
    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        txtTenNhaThau.Text = "";
        txtMaNhaThau.Text = "";
        txtMaNhaThau.Focus();
        btncapnhat.Visible = false;
        btnSave.Visible = true;

        GV.DataSource = ex.GetData("sp_vnttblnhathau_ListGridView");
        GV.DataBind();

        gvGoiThau.DataSource = ex.GetData("sp_vnttblgoithau_List");
        gvGoiThau.DataBind();
    }
    private void gstGetMess(string gstMess, string gstLink)
    {
        if (gstLink == "")
            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), " ", "alert('" + gstMess + "')", true);
        else
            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), " ", "alert('" + gstMess + "');window.location.href='" + gstLink + "'", true);

    }
    protected void GV_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        string ID = e.CommandArgument.ToString();
        hdID.Value = ID;
        Guid gID = new Guid(ID);
        p = new SqlParameter[1];
        p[0] = new SqlParameter("@IDs", gID);
        p[0].SqlDbType = SqlDbType.UniqueIdentifier;

        if (e.CommandName.Equals("Select"))
        {
            DataTable dt = ex.GetData("sp_vnttblnhathau_ListByID", p);
            if (dt.Rows.Count > 0)
            {
                try
                {
                    txtTenNhaThau.Text = dt.Rows[0]["TenNhaThau"].ToString();
                    txtMaNhaThau.Text = dt.Rows[0]["MaNhaThau"].ToString();

                    p = new SqlParameter[1];
                    p[0] = new SqlParameter("@IDs", gID);
                    p[0].SqlDbType = SqlDbType.UniqueIdentifier;
                    
                    dt = ex.GetData("sp_vnttblnhathaugoithau_ListByNhaThau", p);
                    gvGoiThau.DataSource = dt;
                    gvGoiThau.DataBind();
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        CheckBox ck = (CheckBox)gvGoiThau.Rows[i].Cells[1].FindControl("ckGoiThau");
                        if (dt.Rows[i]["checked"].ToString().Equals("1"))
                            ck.Checked = true;
                        else ck.Checked = false;
                    }
                }
                catch (Exception ax)
                {
                    gstGetMess("Có lỗi trong quá trình xem, xem trong file log", "");
                    ex.WriteLog(Server.MapPath("Log.txt"), DateTime.Now.ToString() + "\tNhà thầu\t" + ax.Message);
                }

            }
            btncapnhat.Visible = true;
            btnSave.Visible = false;
        }
        else
            if (e.CommandName.Equals("Del"))
            {

                try
                {
                    ex.ExecuteNonQuerry("sp_vnttblnhathau_Del", p);
                    btnCancel_Click(sender, e);
                    gstGetMess("Xóa thành công", "");
                }
                catch (Exception ax)
                {
                    gstGetMess("Có lỗi trong quá trình xóa, xem trong file log", "");
                    ex.WriteLog(Server.MapPath("Log.txt"), DateTime.Now.ToString() + "\tNhà thầu\t" + ax.Message);

                }

            }
    }
    protected void btncapnhat_Click(object sender, EventArgs e)
    {
        try
        {
            Guid gID = new Guid(hdID.Value);
            p = new SqlParameter[3];
            p[0] = new SqlParameter("@IDs", gID);
            p[0].SqlDbType = SqlDbType.UniqueIdentifier;

            p[1] = new SqlParameter("@TenNhaThau", txtTenNhaThau.Text.Trim());
            p[1].SqlDbType = SqlDbType.NVarChar;
            p[2] = new SqlParameter("@MaNhaThau", txtMaNhaThau.Text.Trim());
            p[2].SqlDbType = SqlDbType.NVarChar;

            ex.ExecuteNonQuerry("sp_vnttblnhathau_Update", p);
            
            for (int j = 0; j < gvGoiThau.Rows.Count; j++)
            {
                CheckBox ck = (CheckBox)gvGoiThau.Rows[j].Cells[1].FindControl("ckGoiThau");
                if (ck.Checked)
                {
                    Label lbID = (Label)gvGoiThau.Rows[j].Cells[0].FindControl("lbID");
                    
                    Guid IDGoiThau = new Guid(lbID.Text.Trim());
                    p = new SqlParameter[3];
                    p[0] = new SqlParameter("@IDs", Guid.NewGuid());
                    p[0].SqlDbType = SqlDbType.UniqueIdentifier;
                    p[1] = new SqlParameter("@IDNhaThau", gID);
                    p[1].SqlDbType = SqlDbType.UniqueIdentifier;
                    p[2] = new SqlParameter("@IDGoiThau", IDGoiThau);
                    p[2].SqlDbType = SqlDbType.UniqueIdentifier;
                    ex.ExecuteNonQuerry("sp_vnttblnhathaugoithau_Add", p);
                }
            }


            btnCancel_Click(sender, e);
            gstGetMess("Cập nhật thành công", "");
        }
        catch (Exception ax)
        {
            gstGetMess("Có lỗi trong quá trình cập nhật, xem trong file log", "");
            ex.WriteLog(Server.MapPath("Log.txt"), DateTime.Now.ToString() + "\tNhà thầu\t" + ax.Message);
        }
    }
}